import { getCategoryAPI } from '@/apis/category'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'
// 封装分类数据业务相关代码
export function useCategory() {
    const categoryData = ref({})
    const route = useRoute()
    const getCategoryData = async (id) => {
        const res = await getCategoryAPI(id)
        // console.log(res);
        categoryData.value = res.result
    }

    onMounted(() => {
        getCategoryData(route.params.id)
    })

    // 目标：路由参数变化的时候，可以把分类数据接口重新发送
    onBeforeRouteUpdate((to) => {
        // console.log("路由变化了");
        getCategoryData(to.params.id)
    })

    return {
        categoryData
    }
}